//
//  DatabaseLogicTest.m
//  IWeighMyself
//
//  Created by Francisco Gindre on 9/8/10.
//  Copyright 2010 Apps Crafter. All rights reserved.
//

#import "DatabaseLogicTest.h"
#import "DatabaseUtils.h"
#import "Weight.h"


@implementation DatabaseLogicTest

#if USE_APPLICATION_UNIT_TEST     // all code under test is in the iPhone Application

- (void) testAppDelegate {
    
    id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];
    STAssertNotNil(yourApplicationDelegate, @"UIApplication failed to find the AppDelegate");
	
    
}

#else                           // all code under test must be linked into the Unit Test bundle

- (void) testDB {
    
	NSString *dbPath = @"/Users/franciscogindre/Dev/iPhoneOS/Prueba XCode4/IWeighMyself/testdb.sql";
	//STFail (@"fasdfa");
	
	NSMutableArray *resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];
	
	STAssertTrue ([resultSet count]>0,@"resultset count is %d",[resultSet count]);
	
	
    STAssertTrue((1+1)==2, @"Compiler isn't feeling well today :-(" );
    
	[dbPath release];
	[resultSet release];
}
-(void) testDelete {
	NSString *dbPath = @"/Users/franciscogindre/Dev/iPhoneOS/Prueba XCode4/IWeighMyself/testdb.sql";
	//STFail (@"fasdfa");
	
	
	
	NSMutableArray *resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];	
	Weight *weightToInsert = [[Weight alloc] initWithID:3 date:[NSDate date] weightInKilos:[NSNumber numberWithInt:80] weightInPounds:[NSNumber numberWithInt:160] isSent:0];
	
	int originalCount = [resultSet count];
	
	STAssertTrue ([resultSet count]>0,@"resultset count is %d",[resultSet count]);
	
	/**
	 insert weight in database
	 */
	[DatabaseUtils insertWeightInDatabase:dbPath weightToInsert:weightToInsert intoTable:@"weight"];

	[resultSet release];
	resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];
	STAssertTrue ((originalCount+1)==[resultSet count],@"resultset count is %d while original count is %d", [resultSet count], originalCount);
	
	
	Weight *weightToDelete = [DatabaseUtils queryNewestWeight:dbPath fromTable:@"weight"];
	
	[DatabaseUtils deleteWeightFromDatabase:dbPath weightToDelete:weightToDelete fromTable:@"weight"];
	
	[resultSet release];
	resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];
	
	
	STAssertTrue (originalCount==[resultSet count],@"resultset count after deletion is %d while original count is %d",[resultSet count], originalCount);
	
				  
    //STAssertTrue((1+1)==2, @"Compiler isn't feeling well today :-(" );
    
	[dbPath release];
	[resultSet release];
	
}

-(void)testUpdate {
	NSString *dbPath = @"/Users/franciscogindre/Dev/iPhoneOS/Prueba XCode4/IWeighMyself/testdb.sql";
	//STFail (@"fasdfa");
	
	
	
	NSMutableArray *resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];	
	//insert weight
	Weight *weightToInsert = [[Weight alloc] initWithID:10 date:[NSDate date] weightInKilos:[NSNumber numberWithInt:80] weightInPounds:[NSNumber numberWithInt:160] isSent:0];
	
	int originalCount = [resultSet count];
	
	STAssertTrue ([resultSet count]>0,@"resultset count is %d",[resultSet count]);
	
	/**
	 insert weight in database
	 */
	[DatabaseUtils insertWeightInDatabase:dbPath weightToInsert:weightToInsert intoTable:@"weight"];
	
	[resultSet release];
	resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];
	STAssertTrue ((originalCount+1)==[resultSet count],@"resultset count is %d while original count is %d", [resultSet count], originalCount);
	
	
	Weight *weightToUpdate = [DatabaseUtils queryNewestWeight:dbPath fromTable:@"weight"];
	
	
	//STAssertTrue (weightToInsert.weightInKilos == weightToUpdate.weightInKilos && weightToInsert.dateLogged == weightToUpdate.dateLogged,@"retrieved weight is not the same it was inserted weight inserted was: %@, weight retrived is: %@",[weightToInsert weightToString],[weightToUpdate weightToString]);
	// update to 90 the kilos
	[weightToUpdate setWeightInKilos:[NSNumber numberWithInt:90]];
	
	
	
	[DatabaseUtils updateEntryFromDatabase:dbPath weight:weightToUpdate fromTable:@"weight"];
	
	[resultSet release];
	resultSet = [DatabaseUtils queryAllEntriesFromDatabase:dbPath fromTable:@"weight"];
	
	
	STAssertTrue (originalCount+1==[resultSet count],@"resultset count after deletion is %d while original count is %d",[resultSet count], originalCount);
	
	Weight *updatedEntry = [DatabaseUtils queryNewestWeight:dbPath fromTable:@"weight"];
	
	
	STAssertTrue ([updatedEntry.weightInKilos floatValue] == [weightToUpdate.weightInKilos floatValue] &&updatedEntry.weightID == weightToUpdate.weightID,@"updated entry does not match weight to update. updated entry is: %@ and weightToUpdate is %@",[updatedEntry weightToString],[weightToUpdate weightToString]);

    //STAssertTrue((1+1)==2, @"Compiler isn't feeling well today :-(" );
    
	[dbPath release];
	[resultSet release];
	
}


#endif


@end
